function cell_fig_vis(aux_data,frame_times,ROIs,saccades,iframe_times,pupil_diam,nbr_frames,downsampleON,cell)

shutterOpen=find(abs(diff(aux_data(1,:)>1))==1);

% Code:
% 1 = feedback
% 2 = playback
% 3 = baseline

if length(nbr_frames)>5
    order=[];
    for xnd=1:length(nbr_frames)
        if nbr_frames(xnd)==20000
            if xnd==length(nbr_frames)
                order(xnd)=3;
            else
                order(xnd)=1;
            end
        else
            order(xnd)=2;
        end
    end
else
    order=[1 2 1 2 3];
end
a=1:length(order);
order(max(a(order==2))+1:end)=3;
nbr_exp=length(order);
nbr_bas=sum(order==3);
bas_exp=[];
for lnd=1:length(nbr_bas)
    bas_exp(lnd)=shutterOpen((nbr_exp*2)-((lnd*2)-1));
end
bas_exp=min(bas_exp);


% % 0=baseline
% % 1=feedback
% % 2=playback
% % 3=feedback mismatch
[velP,velM,velP_ind,velM_ind,velP_raw,velM_raw]=get_vel_ind_from_adata(aux_data);
act=ftfil(ROIs(cell).activity,18,0,10);
act=psmooth(act);
[baseline]=baseline_f(act,frame_times,aux_data,velM_ind,nbr_frames,bas_exp);
raw_act=act/baseline;
max_act=ceil(max(raw_act));

runM=velM_ind(frame_times);
runP=velP_ind(frame_times);
stim_id=round(2*aux_data(3,frame_times));
shutter_openings=find(diff(aux_data(1,:)>1)==1);
[~,pb_onset_frame]=min(abs(frame_times-shutter_openings(2)));
[~,base_onset_frame]=min(abs(frame_times-shutter_openings(length(shutter_openings))));

stim_id(pb_onset_frame:end)=[];
stim_id=logical(stim_id);
runM(pb_onset_frame:end)=[];

shad=[];
vis_fl=[];
for fnd=1:length(runM)
    if runM(fnd)==0
        shad(fnd)=0;
        vis_fl(fnd)=0;
    elseif runM(fnd)==1 & stim_id(fnd)==0 %feedback
        shad(fnd)=1;
        vis_fl(fnd)=1;
    elseif runM(fnd)==1 & stim_id(fnd)==1 %feedback mismatch
        if stim_id(fnd)==7
            %             shad(fnd)=0;
            shad(fnd)=3;
            vis_fl(fnd)=0;
        else
            shad(fnd)=4;
            vis_fl(fnd)=1;
        end
    end
end

runM=velM_ind(frame_times);
runP=velP_ind(frame_times);
stim_id=round(2*aux_data(3,frame_times));



stim_id(base_onset_frame:end)=[];
runM(base_onset_frame:end)=[];
runP(base_onset_frame:end)=[];

stim_id(1:pb_onset_frame-1)=[];
runM(1:pb_onset_frame-1)=[];
runP(1:pb_onset_frame-1)=[];

stim_id=logical(stim_id);

shadP=[];
vis_flP=[];
for fnd=1:length(runM)
    if logical(stim_id(fnd))==0 & runM(fnd)==0 & runP(fnd)==0 | logical(stim_id(fnd))==1 & runM(fnd)==0 & runP(fnd)==0
        shadP(fnd)=0;
        vis_flP(fnd)=0;
    elseif logical(stim_id(fnd))==1 & runM(fnd)==0 & runP(fnd)==1
        if stim_id(fnd)==7
            shadP(fnd)=5;
            vis_flP(fnd)=0;
        else
            shadP(fnd)=6;
            vis_flP(fnd)=1;
        end
    elseif logical(stim_id(fnd))==0 & runM(fnd)==0 & runP(fnd)==1
        shadP(fnd)=2;
        vis_flP(fnd)=1;
    elseif logical(stim_id(fnd))==0 & runM(fnd)==1 & runP(fnd)==1
        shadP(fnd)=1;
        vis_flP(fnd)=1;
    elseif logical(stim_id(fnd))==0 & runM(fnd)==1 & runP(fnd)==0 | logical(stim_id(fnd))==1 & runM(fnd)==1 & runP(fnd)==0
        shadP(fnd)=3;
        vis_flP(fnd)=0;
    elseif logical(stim_id(fnd))==1 & runM(fnd)==1 & runP(fnd)==1
        if stim_id(fnd)==7
            shadP(fnd)=3;
            vis_flP(fnd)=0;
        else
            shadP(fnd)=4;
            vis_flP(fnd)=1;
        end
    end
end

shad_full=shad;
shad_full(length(shad)+1:(length(shad)+length(shadP)))=shadP;
vis_fl_full=vis_fl;
vis_fl_full(length(vis_fl)+1:(length(vis_fl)+length(vis_flP)))=vis_flP;
runM=velM_ind(frame_times);
vis_fl_full(length(vis_fl_full):length(runM))=0;
[diam_aux]=map_eye_data_to_aux(pupil_diam,iframe_times,length(aux_data));
diam_aux=diam_aux(frame_times);
speed_ref=velM(frame_times);
saccades_fr=iframe_times(saccades);
% sacc_aux=zeros(1,length(aux_data));
% for jnd=1:length(saccades_fr)
%     sacc_aux(saccades_fr(jnd):saccades_fr(jnd)+200)=1;
% end
% sacc_aux=sacc_aux(frame_times);

if downsampleON
    shad_full=downsample(shad_full,10);
    runM=downsample(runM,10);
    vis_fl_full=downsample(vis_fl_full,10);
    raw_act=downsample(raw_act,10);
    diam_aux=downsample(diam_aux,10);
    speed_ref=downsample(speed_ref,10);
    %     sacc_aux=downsample(sacc_aux,2);
    new_sacc_inds=[];
    frame_ids=1:length(frame_times);
    down_frames=downsample(frame_times,10);
    for ind=1:length(saccades_fr)
        if ind==length(saccades_fr)
            curr_frames=down_frames(down_frames>saccades_fr(ind));
            new_sacc_inds(ind)=frame_ids(down_frames==min(curr_frames));
        else
            curr_frames=down_frames(down_frames>saccades_fr(ind)&down_frames<saccades_fr(ind+1));
            new_sacc_inds(ind)=frame_ids(down_frames==min(curr_frames));
        end
    end
end

% sacc_aux=find(diff(sacc_aux)==1);
% tmp_saccades=sacc_aux;

if downsampleON
    nbr_frames=nbr_frames/4;
    nbr_frames=floor(nbr_frames/10);
    if length(nbr_frames)>5
        order=[];
        for xnd=1:length(nbr_frames)
            if nbr_frames(xnd)==500
                if xnd==length(nbr_frames)
                    order(xnd)=3;
                else
                    order(xnd)=1;
                end
            else
                order(xnd)=2;
            end
        end
    else
        order=[1 2 1 2 3];
    end
else
    nbr_frames=nbr_frames/4;
    if length(nbr_frames)>5
        order=[];
        for xnd=1:length(nbr_frames)
            if nbr_frames(xnd)==5000
                if xnd==length(nbr_frames)
                    order(xnd)=3;
                else
                    order(xnd)=1;
                end
            else
                order(xnd)=2;
            end
        end
    else
        order=[1 2 1 2 3];
    end
end
a=1:length(order);
order(max(a(order==2))+1:end)=3;
inds=1:length(order);
fb_inds=inds(order==1);
b_inds=inds(order==3);
cum_frames=[];
for xnd=1:length(nbr_frames)-1
    if xnd==1
        cum_frames(xnd)=0+nbr_frames(xnd);
    else
        cum_frames(xnd)=cum_frames(xnd-1)+nbr_frames(xnd);
    end
end



split=[];
split(1)=0;
split(2)=sum(cum_frames(fb_inds(2)-1));
split(3)=sum(cum_frames(min(b_inds)-1));
split(4)=sum(nbr_frames);

col{1}=[0.8 0.8 0.8];
col{2}=[0.3 0.7 0.3];
col{3}=[1 0.5 0];
col{4}=[1 1 0.5];
col{5}=[0.9 0.2 0.2];
col{6}=[0 1 1];

for vnd=1:3
    if vnd<3
        cur_shad_full=shad_full(split(vnd)+1:split(vnd+1));
    end
    cur_runM=runM(split(vnd)+1:split(vnd+1));
    cur_vis_fl_full=vis_fl_full(split(vnd)+1:split(vnd+1));
    cur_raw_act=raw_act(split(vnd)+1:split(vnd+1));
    
    if vnd>1
        cur_tmp_saccades=new_sacc_inds(new_sacc_inds>=split(vnd)+1&new_sacc_inds<=split(vnd+1));
        cur_tmp_saccades=cur_tmp_saccades-split(vnd);
    else
        cur_tmp_saccades=new_sacc_inds(new_sacc_inds>=split(vnd)+1&new_sacc_inds<=split(vnd+1));
    end
    
    cur_diam=diam_aux(split(vnd)+1:split(vnd+1));
    cur_speed_ref=speed_ref(split(vnd)+1:split(vnd+1));
    
    figure('color','white');
    set(gcf,'Position',[57 600-(300*(vnd-1)) 3423 186])
    hold on
    set(gca,'Position',[0.01 0.01 0.99 0.99])
    ylim([0 max_act])
    xlim([0 length(cur_raw_act)])
    if vnd<3
        for hnd=1:6
            area([1:length(cur_shad_full)],50*(cur_shad_full==hnd),'facecolor',col{hnd},'linestyle','none')
        end
    end
    hold on
    plot(cur_runM/5,'color','red','LineWidth',1.2)
    plot((cur_vis_fl_full/5)+0.3,'color',[0 0.3 0],'LineWidth',1.2)
    plot((cur_speed_ref*10)+0.6,'color',[0 0.3 1]);
    if ~isempty(cur_tmp_saccades)
        for fnd=1:length(cur_tmp_saccades)
            hold on
            rectangle('position',[cur_tmp_saccades(fnd) max_act-0.3 3 0.2],'facecolor',[0 0 0],'linestyle','none')
        end
    end
    hold on
    plot(cur_diam/(max(diam_aux)/3)+(max_act-3.2),'color','blue')
    plot(cur_raw_act,'color','black','LineWidth',1.5)
    axis off
    drawnow
end
